<!DOCTYPE html>
<html lang=en

  >

  <head>
    <!-- so meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="HandheldFriendly" content="True">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=5" />
    <meta name="description" content="String创建对象的个数及其原理创建对象的个数示例1：字面量+字面量题目：以下语句创建了几个对象？？1String str &#x3D; &quot;abc&quot; + &quot;def&quot;;  答案：1个编译期已经常量拼为”abcdef”，放到常量池，变量str获得是”abcdef”。示例2：字面量+对象+字面量题目：以下语句创建了几个对象？？12String s1 &#x3D; “abc”;Str">
<meta property="og:type" content="article">
<meta property="og:title" content="技术">
<meta property="og:url" content="https://liushuaijun.gitee.io/mrl/2023/08/28/%E6%8A%80%E6%9C%AF/index.html">
<meta property="og:site_name" content="一个有温度的小流年">
<meta property="og:description" content="String创建对象的个数及其原理创建对象的个数示例1：字面量+字面量题目：以下语句创建了几个对象？？1String str &#x3D; &quot;abc&quot; + &quot;def&quot;;  答案：1个编译期已经常量拼为”abcdef”，放到常量池，变量str获得是”abcdef”。示例2：字面量+对象+字面量题目：以下语句创建了几个对象？？12String s1 &#x3D; “abc”;Str">
<meta property="og:locale" content="en_US">
<meta property="article:published_time" content="2023-08-28T08:11:05.000Z">
<meta property="article:modified_time" content="2023-08-29T10:25:56.835Z">
<meta property="article:author" content="小流年">
<meta name="twitter:card" content="summary">
    
    
      
        
          <link rel="shortcut icon" href="/mrl/images/favicon.ico">
        
      
      
        
          <link rel="icon" type="image/png" href="/mrl/images/favicon-192x192.png" sizes="192x192">
        
      
      
        
          <link rel="apple-touch-icon" sizes="180x180" href="/mrl/images/apple-touch-icon.png">
        
      
    
    <!-- title -->
    <title>技术</title>
    <!-- async scripts -->
    <!-- Google Analytics -->


    <!-- Umami Analytics -->


    <!-- styles -->
    
<link rel="stylesheet" href="/mrl/css/style.css">

    <!-- persian styles -->
    
    <!-- rss -->
    
    
	<!-- mathjax -->
	
<meta name="generator" content="Hexo 6.3.0"></head>


    <body class="max-width mx-auto px3 ltr">
      
        <div id="header-post">
  <a id="menu-icon" href="#" aria-label="Menu"><i class="fa-solid fa-bars fa-lg"></i></a>
  <a id="menu-icon-tablet" href="#" aria-label="Menu"><i class="fa-solid fa-bars fa-lg"></i></a>
  <a id="top-icon-tablet" href="#" aria-label="Top" onclick="$('html, body').animate({ scrollTop: 0 }, 'fast');" style="display:none;"><i class="fa-solid fa-chevron-up fa-lg"></i></a>
  <span id="menu">
    <span id="nav">
      <ul>
        <!--
       --><li><a href="/mrl/search/">搜索</a></li><!--
     --><!--
       --><li><a href="/mrl/">首页</a></li><!--
     --><!--
       --><li><a href="/mrl/categories/">个人</a></li><!--
     --><!--
       --><li><a href="/mrl/articles/">归档</a></li><!--
     --><!--
       --><li><a href="/mrl/product/">产品</a></li><!--
     -->
      </ul>
    </span>
    <br/>
    <span id="actions">
      <ul>
        
        <li><a class="icon" aria-label="Previous post" href="/mrl/2023/08/29/Linux/"><i class="fa-solid fa-chevron-left" aria-hidden="true" onmouseover="$('#i-prev').toggle();" onmouseout="$('#i-prev').toggle();"></i></a></li>
        
        
        <li><a class="icon" aria-label="Next post" href="/mrl/2023/08/28/%E4%B8%80%E6%9E%9A%E4%B9%90%E4%BA%8E%E5%88%86%E4%BA%AB%E6%8A%80%E6%9C%AF%E4%B8%8E%E5%BF%AB%E4%B9%90%E7%9A%84%E5%8D%9A%E4%B8%BB/"><i class="fa-solid fa-chevron-right" aria-hidden="true" onmouseover="$('#i-next').toggle();" onmouseout="$('#i-next').toggle();"></i></a></li>
        
        <li><a class="icon" aria-label="Back to top" href="#" onclick="$('html, body').animate({ scrollTop: 0 }, 'fast');"><i class="fa-solid fa-chevron-up" aria-hidden="true" onmouseover="$('#i-top').toggle();" onmouseout="$('#i-top').toggle();"></i></a></li>
        <li><a class="icon" aria-label="مشاركة المنشور" href="#"><i class="fa-solid fa-share-alt" aria-hidden="true" onmouseover="$('#i-share').toggle();" onmouseout="$('#i-share').toggle();" onclick="$('#share').toggle();return false;"></i></a></li>
      </ul>
      <span id="i-prev" class="info" style="display:none;">Previous post</span>
      <span id="i-next" class="info" style="display:none;">Next post</span>
      <span id="i-top" class="info" style="display:none;">Back to top</span>
      <span id="i-share" class="info" style="display:none;">مشاركة المنشور</span>
    </span>
    <br/>
    <div id="share" style="display: none">
      <ul>
  <li><a class="icon" target="_blank" rel="noopener" href="http://www.facebook.com/sharer.php?u=https://liushuaijun.gitee.io/mrl/2023/08/28/%E6%8A%80%E6%9C%AF/"><i class="fab fa-facebook " aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="https://twitter.com/share?url=https://liushuaijun.gitee.io/mrl/2023/08/28/%E6%8A%80%E6%9C%AF/&text=技术"><i class="fab fa-twitter " aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="http://www.linkedin.com/shareArticle?url=https://liushuaijun.gitee.io/mrl/2023/08/28/%E6%8A%80%E6%9C%AF/&title=技术"><i class="fab fa-linkedin " aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="https://pinterest.com/pin/create/bookmarklet/?url=https://liushuaijun.gitee.io/mrl/2023/08/28/%E6%8A%80%E6%9C%AF/&is_video=false&description=技术"><i class="fab fa-pinterest " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="mailto:?subject=技术&body=Check out this article: https://liushuaijun.gitee.io/mrl/2023/08/28/%E6%8A%80%E6%9C%AF/"><i class="fa-solid fa-envelope " aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="https://getpocket.com/save?url=https://liushuaijun.gitee.io/mrl/2023/08/28/%E6%8A%80%E6%9C%AF/&title=技术"><i class="fab fa-get-pocket " aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="http://reddit.com/submit?url=https://liushuaijun.gitee.io/mrl/2023/08/28/%E6%8A%80%E6%9C%AF/&title=技术"><i class="fab fa-reddit " aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="http://www.stumbleupon.com/submit?url=https://liushuaijun.gitee.io/mrl/2023/08/28/%E6%8A%80%E6%9C%AF/&title=技术"><i class="fab fa-stumbleupon " aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="http://digg.com/submit?url=https://liushuaijun.gitee.io/mrl/2023/08/28/%E6%8A%80%E6%9C%AF/&title=技术"><i class="fab fa-digg " aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="http://www.tumblr.com/share/link?url=https://liushuaijun.gitee.io/mrl/2023/08/28/%E6%8A%80%E6%9C%AF/&name=技术&description="><i class="fab fa-tumblr " aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="https://news.ycombinator.com/submitlink?u=https://liushuaijun.gitee.io/mrl/2023/08/28/%E6%8A%80%E6%9C%AF/&t=技术"><i class="fab fa-hacker-news " aria-hidden="true"></i></a></li>
</ul>

    </div>
    
    
      <div id="toc">
        <ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#String%E5%88%9B%E5%BB%BA%E5%AF%B9%E8%B1%A1%E7%9A%84%E4%B8%AA%E6%95%B0%E5%8F%8A%E5%85%B6%E5%8E%9F%E7%90%86"><span class="toc-number">1.</span> <span class="toc-text">String创建对象的个数及其原理</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%88%9B%E5%BB%BA%E5%AF%B9%E8%B1%A1%E7%9A%84%E4%B8%AA%E6%95%B0"><span class="toc-number">1.1.</span> <span class="toc-text">创建对象的个数</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%A4%BA%E4%BE%8B1%EF%BC%9A%E5%AD%97%E9%9D%A2%E9%87%8F-%E5%AD%97%E9%9D%A2%E9%87%8F"><span class="toc-number">1.2.</span> <span class="toc-text">示例1：字面量+字面量</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%A2%98%E7%9B%AE%EF%BC%9A%E4%BB%A5%E4%B8%8B%E8%AF%AD%E5%8F%A5%E5%88%9B%E5%BB%BA%E4%BA%86%E5%87%A0%E4%B8%AA%E5%AF%B9%E8%B1%A1%EF%BC%9F%EF%BC%9F"><span class="toc-number">1.2.1.</span> <span class="toc-text">题目：以下语句创建了几个对象？？</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%AD%94%E6%A1%88%EF%BC%9A1%E4%B8%AA"><span class="toc-number">1.2.2.</span> <span class="toc-text">答案：1个</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%BC%96%E8%AF%91%E6%9C%9F%E5%B7%B2%E7%BB%8F%E5%B8%B8%E9%87%8F%E6%8B%BC%E4%B8%BA%E2%80%9Dabcdef%E2%80%9D%EF%BC%8C%E6%94%BE%E5%88%B0%E5%B8%B8%E9%87%8F%E6%B1%A0%EF%BC%8C%E5%8F%98%E9%87%8Fstr%E8%8E%B7%E5%BE%97%E6%98%AF%E2%80%9Dabcdef%E2%80%9D%E3%80%82"><span class="toc-number">1.2.3.</span> <span class="toc-text">编译期已经常量拼为”abcdef”，放到常量池，变量str获得是”abcdef”。</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%A4%BA%E4%BE%8B2%EF%BC%9A%E5%AD%97%E9%9D%A2%E9%87%8F-%E5%AF%B9%E8%B1%A1-%E5%AD%97%E9%9D%A2%E9%87%8F"><span class="toc-number">1.3.</span> <span class="toc-text">示例2：字面量+对象+字面量</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%A2%98%E7%9B%AE%EF%BC%9A%E4%BB%A5%E4%B8%8B%E8%AF%AD%E5%8F%A5%E5%88%9B%E5%BB%BA%E4%BA%86%E5%87%A0%E4%B8%AA%E5%AF%B9%E8%B1%A1%EF%BC%9F%EF%BC%9F-1"><span class="toc-number">1.3.1.</span> <span class="toc-text">题目：以下语句创建了几个对象？？</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%AD%94%E6%A1%88%EF%BC%9A%E5%88%9B%E5%BB%BA3%E4%B8%AA%E5%AF%B9%E8%B1%A1%E3%80%82%E5%B8%B8%E9%87%8F%E6%B1%A0%E4%B8%AD2%E4%B8%AA%EF%BC%9Aabc%EF%BC%8Cdef%EF%BC%9B%E5%A0%86%E4%B8%AD1%E4%B8%AA%EF%BC%9Aabcabcdef"><span class="toc-number">1.3.2.</span> <span class="toc-text">答案：创建3个对象。常量池中2个：abc，def；堆中1个：abcabcdef</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%A7%A3%E6%9E%90"><span class="toc-number">1.3.3.</span> <span class="toc-text">解析</span></a></li></ol></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E7%A8%8B%E5%BA%8F%E9%AA%8C%E8%AF%81"><span class="toc-number">2.</span> <span class="toc-text">程序验证</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E8%BF%90%E8%A1%8C%E7%BB%93%E6%9E%9C"><span class="toc-number">3.</span> <span class="toc-text">运行结果</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%A4%BA%E4%BE%8B3%EF%BC%9Anew-String-%E2%80%9Cxx%E2%80%9D-new-String-%E2%80%9Cxx%E2%80%9D"><span class="toc-number">3.1.</span> <span class="toc-text">示例3：new String(“xx”) + new String(“xx”)</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%A2%98%E7%9B%AE%EF%BC%9A%E4%BB%A5%E4%B8%8B%E8%AF%AD%E5%8F%A5%E5%88%9B%E5%BB%BA%E4%BA%86%E5%87%A0%E4%B8%AA%E5%AF%B9%E8%B1%A1%EF%BC%9F%EF%BC%9F-2"><span class="toc-number">3.1.1.</span> <span class="toc-text">题目：以下语句创建了几个对象？？</span></a></li></ol></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E7%AD%94%E6%A1%88"><span class="toc-number">4.</span> <span class="toc-text">答案</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%88%9B%E5%BB%BA4%E4%B8%AAString%E5%AF%B9%E8%B1%A1"><span class="toc-number">4.0.1.</span> <span class="toc-text">创建4个String对象</span></a></li></ol></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%88%86%E6%9E%90"><span class="toc-number">5.</span> <span class="toc-text">分析</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%A4%BA%E4%BE%8B4%EF%BC%9A%E5%AD%97%E9%9D%A2%E9%87%8F-new-String-%E2%80%9Cxx%E2%80%9D"><span class="toc-number">5.1.</span> <span class="toc-text">示例4：字面量+new String(“xx”)</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%A2%98%E7%9B%AE%EF%BC%9A%E4%BB%A5%E4%B8%8B%E8%AF%AD%E5%8F%A5%E5%88%9B%E5%BB%BA%E4%BA%86%E5%87%A0%E4%B8%AA%E5%AF%B9%E8%B1%A1%EF%BC%9F%EF%BC%9F-3"><span class="toc-number">5.1.1.</span> <span class="toc-text">题目：以下语句创建了几个对象？？</span></a></li></ol></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E7%AD%94%E6%A1%88-1"><span class="toc-number">6.</span> <span class="toc-text">答案</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%88%9B%E5%BB%BA4%E4%B8%AAString%E5%AF%B9%E8%B1%A1-1"><span class="toc-number">6.0.1.</span> <span class="toc-text">创建4个String对象</span></a></li></ol></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%88%86%E6%9E%90-1"><span class="toc-number">7.</span> <span class="toc-text">分析</span></a></li></ol>
      </div>
    
  </span>
</div>

          
            <div class="content index py4 ">
              
                    <article class="post h-entry" itemscope itemtype="http://schema.org/BlogPosting">
  <header>
    
    <h1 class="posttitle p-name" itemprop="name headline">
        技术
    </h1>



    <div class="meta">
      <span class="author p-author h-card" itemprop="author" itemscope itemtype="http://schema.org/Person">
        <span class="p-name" itemprop="name">小流年</span>
      </span>
      
    <div class="postdate">
      
        <time datetime="2023-08-28T08:11:05.000Z" class="dt-published" itemprop="datePublished">2023-08-28</time>
        
      
    </div>


      
    <div class="article-category">
        <i class="fa-solid fa-archive"></i>
        <a class="category-link" href="/mrl/categories/%E6%8A%80%E6%9C%AF/">技术</a>
    </div>


      

    </div>
  </header>
  

  <div class="content e-content" itemprop="articleBody">
    <h1 id="String创建对象的个数及其原理"><a href="#String创建对象的个数及其原理" class="headerlink" title="String创建对象的个数及其原理"></a>String创建对象的个数及其原理</h1><h2 id="创建对象的个数"><a href="#创建对象的个数" class="headerlink" title="创建对象的个数"></a>创建对象的个数</h2><h2 id="示例1：字面量-字面量"><a href="#示例1：字面量-字面量" class="headerlink" title="示例1：字面量+字面量"></a>示例1：字面量+字面量</h2><h3 id="题目：以下语句创建了几个对象？？"><a href="#题目：以下语句创建了几个对象？？" class="headerlink" title="题目：以下语句创建了几个对象？？"></a>题目：以下语句创建了几个对象？？</h3><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">String str = &quot;abc&quot; + &quot;def&quot;;</span><br></pre></td></tr></table></figure>

<h3 id="答案：1个"><a href="#答案：1个" class="headerlink" title="答案：1个"></a>答案：1个</h3><h3 id="编译期已经常量拼为”abcdef”，放到常量池，变量str获得是”abcdef”。"><a href="#编译期已经常量拼为”abcdef”，放到常量池，变量str获得是”abcdef”。" class="headerlink" title="编译期已经常量拼为”abcdef”，放到常量池，变量str获得是”abcdef”。"></a>编译期已经常量拼为”abcdef”，放到常量池，变量str获得是”abcdef”。</h3><h2 id="示例2：字面量-对象-字面量"><a href="#示例2：字面量-对象-字面量" class="headerlink" title="示例2：字面量+对象+字面量"></a>示例2：字面量+对象+字面量</h2><h3 id="题目：以下语句创建了几个对象？？-1"><a href="#题目：以下语句创建了几个对象？？-1" class="headerlink" title="题目：以下语句创建了几个对象？？"></a>题目：以下语句创建了几个对象？？</h3><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">String s1 = “abc”;</span><br><span class="line">String s2 = ”abc” + s1 + ”def” ;</span><br></pre></td></tr></table></figure>

<h3 id="答案：创建3个对象。常量池中2个：abc，def；堆中1个：abcabcdef"><a href="#答案：创建3个对象。常量池中2个：abc，def；堆中1个：abcabcdef" class="headerlink" title="答案：创建3个对象。常量池中2个：abc，def；堆中1个：abcabcdef"></a>答案：创建3个对象。常量池中2个：abc，def；堆中1个：abcabcdef</h3><h3 id="解析"><a href="#解析" class="headerlink" title="解析"></a>解析</h3><p>String s1 &#x3D; “abc”;：创建1个对象：生成了一个String对象”abc”并放入常量池（其中的字符串池）中，定义了一个String引用变量s1并指向”abc”。</p>
<p>String s2 &#x3D;”abc”+s1+”def”;创建2个对象：”abc”已经在池中了，直接从池中取出；s1是引用地址，即：s1&#x3D;&#x3D;”abc”为true；创建了一个”def”的String对象并放入池中。创建一个”abcabcdef”的String对象存放于堆（而不是常量池）。</p>
<h1 id="程序验证"><a href="#程序验证" class="headerlink" title="程序验证"></a>程序验证</h1><pre><code><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">package org.example.a;</span><br><span class="line">  public class Demo &#123;</span><br><span class="line">    public static void main(String[] args) &#123;</span><br><span class="line">     String s1 = &quot;abc&quot;;</span><br><span class="line">     String s2 = &quot;abc&quot;;</span><br><span class="line">     System.out.println(s1 == s2);</span><br><span class="line">     String s3 = &quot;abc&quot; + s1 + &quot;def&quot;;</span><br><span class="line">     String s4 = &quot;abcabcdef&quot;;</span><br><span class="line">     System.out.println(s3 == s4);</span><br><span class="line">     String s5 = s3.intern();</span><br><span class="line">     System.out.println(s4 == s5);</span><br><span class="line">   &#125;</span><br><span class="line"> &#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>
</code></pre>
<h1 id="运行结果"><a href="#运行结果" class="headerlink" title="运行结果"></a>运行结果</h1><pre><code><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">true</span><br><span class="line">false</span><br><span class="line">true</span><br></pre></td></tr></table></figure>
</code></pre>
<h2 id="示例3：new-String-“xx”-new-String-“xx”"><a href="#示例3：new-String-“xx”-new-String-“xx”" class="headerlink" title="示例3：new String(“xx”) + new String(“xx”)"></a>示例3：new String(“xx”) + new String(“xx”)</h2><h3 id="题目：以下语句创建了几个对象？？-2"><a href="#题目：以下语句创建了几个对象？？-2" class="headerlink" title="题目：以下语句创建了几个对象？？"></a>题目：以下语句创建了几个对象？？</h3><pre><code><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">String s = new String(“abc”) + new String(“abc”);</span><br></pre></td></tr></table></figure>
</code></pre>
<h1 id="答案"><a href="#答案" class="headerlink" title="答案"></a>答案</h1><h3 id="创建4个String对象"><a href="#创建4个String对象" class="headerlink" title="创建4个String对象"></a>创建4个String对象</h3><h1 id="分析"><a href="#分析" class="headerlink" title="分析"></a>分析</h1><ol>
<li>JVM先在String池中创建1个String对象存储”abc”</li>
<li>遇到new关键字，再在内存堆上创建1个String对象存储”abc”，其char value[]则指向常量池中的char value[]；</li>
<li>String池中已有”abc”的对象，所以第二个new语句不在String池中创建对象，只在内存堆上创建1个String对象；</li>
<li>两个字符串相加会在堆上创建1个String对象”abcabc”。（因为没有显式使用双引号指定，也没有调用intern，所以字符串池里边目前没有“abcabc”对象）</li>
</ol>
<h2 id="示例4：字面量-new-String-“xx”"><a href="#示例4：字面量-new-String-“xx”" class="headerlink" title="示例4：字面量+new String(“xx”)"></a>示例4：字面量+new String(“xx”)</h2><h3 id="题目：以下语句创建了几个对象？？-3"><a href="#题目：以下语句创建了几个对象？？-3" class="headerlink" title="题目：以下语句创建了几个对象？？"></a>题目：以下语句创建了几个对象？？</h3><pre><code><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">String s = “abc” + new String(“def”);</span><br></pre></td></tr></table></figure>
</code></pre>
<h1 id="答案-1"><a href="#答案-1" class="headerlink" title="答案"></a>答案</h1><h3 id="创建4个String对象-1"><a href="#创建4个String对象-1" class="headerlink" title="创建4个String对象"></a>创建4个String对象</h3><h1 id="分析-1"><a href="#分析-1" class="headerlink" title="分析"></a>分析</h1><ol>
<li>JVM先在String池中创建2个String对象存储”abc”和”def”</li>
<li>遇到new关键字，再在内存堆上创建1个String对象存储”def”，其char value[]则指向常量池中的char value[]；</li>
<li>两个字符串相加会在堆上创建1个String对象”abcdef”。（因为没有显式使用双引号指定，也没有调用intern，所以字符串池里边目前没有“abcdef”对象）</li>
</ol>

  </div>
</article>



                      
                        <div id="footer-post-container">
  <div id="footer-post">

    <div id="nav-footer" style="display: none">
      <ul>
        
          <li><a href="/mrl/search/">搜索</a></li>
        
          <li><a href="/mrl/">首页</a></li>
        
          <li><a href="/mrl/categories/">个人</a></li>
        
          <li><a href="/mrl/articles/">归档</a></li>
        
          <li><a href="/mrl/product/">产品</a></li>
        
      </ul>
    </div>

    
    
      <div id="toc-footer" style="display: none">
        <ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#String%E5%88%9B%E5%BB%BA%E5%AF%B9%E8%B1%A1%E7%9A%84%E4%B8%AA%E6%95%B0%E5%8F%8A%E5%85%B6%E5%8E%9F%E7%90%86"><span class="toc-number">1.</span> <span class="toc-text">String创建对象的个数及其原理</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%88%9B%E5%BB%BA%E5%AF%B9%E8%B1%A1%E7%9A%84%E4%B8%AA%E6%95%B0"><span class="toc-number">1.1.</span> <span class="toc-text">创建对象的个数</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%A4%BA%E4%BE%8B1%EF%BC%9A%E5%AD%97%E9%9D%A2%E9%87%8F-%E5%AD%97%E9%9D%A2%E9%87%8F"><span class="toc-number">1.2.</span> <span class="toc-text">示例1：字面量+字面量</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%A2%98%E7%9B%AE%EF%BC%9A%E4%BB%A5%E4%B8%8B%E8%AF%AD%E5%8F%A5%E5%88%9B%E5%BB%BA%E4%BA%86%E5%87%A0%E4%B8%AA%E5%AF%B9%E8%B1%A1%EF%BC%9F%EF%BC%9F"><span class="toc-number">1.2.1.</span> <span class="toc-text">题目：以下语句创建了几个对象？？</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%AD%94%E6%A1%88%EF%BC%9A1%E4%B8%AA"><span class="toc-number">1.2.2.</span> <span class="toc-text">答案：1个</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%BC%96%E8%AF%91%E6%9C%9F%E5%B7%B2%E7%BB%8F%E5%B8%B8%E9%87%8F%E6%8B%BC%E4%B8%BA%E2%80%9Dabcdef%E2%80%9D%EF%BC%8C%E6%94%BE%E5%88%B0%E5%B8%B8%E9%87%8F%E6%B1%A0%EF%BC%8C%E5%8F%98%E9%87%8Fstr%E8%8E%B7%E5%BE%97%E6%98%AF%E2%80%9Dabcdef%E2%80%9D%E3%80%82"><span class="toc-number">1.2.3.</span> <span class="toc-text">编译期已经常量拼为”abcdef”，放到常量池，变量str获得是”abcdef”。</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%A4%BA%E4%BE%8B2%EF%BC%9A%E5%AD%97%E9%9D%A2%E9%87%8F-%E5%AF%B9%E8%B1%A1-%E5%AD%97%E9%9D%A2%E9%87%8F"><span class="toc-number">1.3.</span> <span class="toc-text">示例2：字面量+对象+字面量</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%A2%98%E7%9B%AE%EF%BC%9A%E4%BB%A5%E4%B8%8B%E8%AF%AD%E5%8F%A5%E5%88%9B%E5%BB%BA%E4%BA%86%E5%87%A0%E4%B8%AA%E5%AF%B9%E8%B1%A1%EF%BC%9F%EF%BC%9F-1"><span class="toc-number">1.3.1.</span> <span class="toc-text">题目：以下语句创建了几个对象？？</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%AD%94%E6%A1%88%EF%BC%9A%E5%88%9B%E5%BB%BA3%E4%B8%AA%E5%AF%B9%E8%B1%A1%E3%80%82%E5%B8%B8%E9%87%8F%E6%B1%A0%E4%B8%AD2%E4%B8%AA%EF%BC%9Aabc%EF%BC%8Cdef%EF%BC%9B%E5%A0%86%E4%B8%AD1%E4%B8%AA%EF%BC%9Aabcabcdef"><span class="toc-number">1.3.2.</span> <span class="toc-text">答案：创建3个对象。常量池中2个：abc，def；堆中1个：abcabcdef</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%A7%A3%E6%9E%90"><span class="toc-number">1.3.3.</span> <span class="toc-text">解析</span></a></li></ol></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E7%A8%8B%E5%BA%8F%E9%AA%8C%E8%AF%81"><span class="toc-number">2.</span> <span class="toc-text">程序验证</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E8%BF%90%E8%A1%8C%E7%BB%93%E6%9E%9C"><span class="toc-number">3.</span> <span class="toc-text">运行结果</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%A4%BA%E4%BE%8B3%EF%BC%9Anew-String-%E2%80%9Cxx%E2%80%9D-new-String-%E2%80%9Cxx%E2%80%9D"><span class="toc-number">3.1.</span> <span class="toc-text">示例3：new String(“xx”) + new String(“xx”)</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%A2%98%E7%9B%AE%EF%BC%9A%E4%BB%A5%E4%B8%8B%E8%AF%AD%E5%8F%A5%E5%88%9B%E5%BB%BA%E4%BA%86%E5%87%A0%E4%B8%AA%E5%AF%B9%E8%B1%A1%EF%BC%9F%EF%BC%9F-2"><span class="toc-number">3.1.1.</span> <span class="toc-text">题目：以下语句创建了几个对象？？</span></a></li></ol></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E7%AD%94%E6%A1%88"><span class="toc-number">4.</span> <span class="toc-text">答案</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%88%9B%E5%BB%BA4%E4%B8%AAString%E5%AF%B9%E8%B1%A1"><span class="toc-number">4.0.1.</span> <span class="toc-text">创建4个String对象</span></a></li></ol></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%88%86%E6%9E%90"><span class="toc-number">5.</span> <span class="toc-text">分析</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%A4%BA%E4%BE%8B4%EF%BC%9A%E5%AD%97%E9%9D%A2%E9%87%8F-new-String-%E2%80%9Cxx%E2%80%9D"><span class="toc-number">5.1.</span> <span class="toc-text">示例4：字面量+new String(“xx”)</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%A2%98%E7%9B%AE%EF%BC%9A%E4%BB%A5%E4%B8%8B%E8%AF%AD%E5%8F%A5%E5%88%9B%E5%BB%BA%E4%BA%86%E5%87%A0%E4%B8%AA%E5%AF%B9%E8%B1%A1%EF%BC%9F%EF%BC%9F-3"><span class="toc-number">5.1.1.</span> <span class="toc-text">题目：以下语句创建了几个对象？？</span></a></li></ol></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E7%AD%94%E6%A1%88-1"><span class="toc-number">6.</span> <span class="toc-text">答案</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%88%9B%E5%BB%BA4%E4%B8%AAString%E5%AF%B9%E8%B1%A1-1"><span class="toc-number">6.0.1.</span> <span class="toc-text">创建4个String对象</span></a></li></ol></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%88%86%E6%9E%90-1"><span class="toc-number">7.</span> <span class="toc-text">分析</span></a></li></ol>
      </div>
    

    <div id="share-footer" style="display: none">
      <ul>
  <li><a class="icon" target="_blank" rel="noopener" href="http://www.facebook.com/sharer.php?u=https://liushuaijun.gitee.io/mrl/2023/08/28/%E6%8A%80%E6%9C%AF/"><i class="fab fa-facebook fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="https://twitter.com/share?url=https://liushuaijun.gitee.io/mrl/2023/08/28/%E6%8A%80%E6%9C%AF/&text=技术"><i class="fab fa-twitter fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="http://www.linkedin.com/shareArticle?url=https://liushuaijun.gitee.io/mrl/2023/08/28/%E6%8A%80%E6%9C%AF/&title=技术"><i class="fab fa-linkedin fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="https://pinterest.com/pin/create/bookmarklet/?url=https://liushuaijun.gitee.io/mrl/2023/08/28/%E6%8A%80%E6%9C%AF/&is_video=false&description=技术"><i class="fab fa-pinterest fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="mailto:?subject=技术&body=Check out this article: https://liushuaijun.gitee.io/mrl/2023/08/28/%E6%8A%80%E6%9C%AF/"><i class="fa-solid fa-envelope fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="https://getpocket.com/save?url=https://liushuaijun.gitee.io/mrl/2023/08/28/%E6%8A%80%E6%9C%AF/&title=技术"><i class="fab fa-get-pocket fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="http://reddit.com/submit?url=https://liushuaijun.gitee.io/mrl/2023/08/28/%E6%8A%80%E6%9C%AF/&title=技术"><i class="fab fa-reddit fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="http://www.stumbleupon.com/submit?url=https://liushuaijun.gitee.io/mrl/2023/08/28/%E6%8A%80%E6%9C%AF/&title=技术"><i class="fab fa-stumbleupon fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="http://digg.com/submit?url=https://liushuaijun.gitee.io/mrl/2023/08/28/%E6%8A%80%E6%9C%AF/&title=技术"><i class="fab fa-digg fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="http://www.tumblr.com/share/link?url=https://liushuaijun.gitee.io/mrl/2023/08/28/%E6%8A%80%E6%9C%AF/&name=技术&description="><i class="fab fa-tumblr fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="https://news.ycombinator.com/submitlink?u=https://liushuaijun.gitee.io/mrl/2023/08/28/%E6%8A%80%E6%9C%AF/&t=技术"><i class="fab fa-hacker-news fa-lg" aria-hidden="true"></i></a></li>
</ul>

    </div>

    <div id="actions-footer">
        <a id="menu" class="icon" href="#" onclick="$('#nav-footer').toggle();return false;"><i class="fa-solid fa-bars fa-lg" aria-hidden="true"></i> Menu</a>
        
          <a id="toc" class="icon" href="#" onclick="$('#toc-footer').toggle();return false;"><i class="fa-solid fa-list fa-lg" aria-hidden="true"></i> TOC</a>
        
        <a id="share" class="icon" href="#" onclick="$('#share-footer').toggle();return false;"><i class="fa-solid fa-share-alt fa-lg" aria-hidden="true"></i> Share</a>
        <a id="top" style="display:none" class="icon" href="#" onclick="$('html, body').animate({ scrollTop: 0 }, 'fast');"><i class="fa-solid fa-chevron-up fa-lg" aria-hidden="true"></i> Top</a>
    </div>

  </div>
</div>

                          
                            <footer id="footer">
  <div class="footer-left">
    Copyright &copy;
    
    
    2020-2023
    小流年
  </div>
  <div class="footer-right">
    <nav>
      <ul>
        <!--
       --><li><a href="/mrl/search/">搜索</a></li><!--
     --><!--
       --><li><a href="/mrl/">首页</a></li><!--
     --><!--
       --><li><a href="/mrl/categories/">个人</a></li><!--
     --><!--
       --><li><a href="/mrl/articles/">归档</a></li><!--
     --><!--
       --><li><a href="/mrl/product/">产品</a></li><!--
     -->
      </ul>
    </nav>
  </div>
  
  <div class="busuanzi">
    
      <!-- 不蒜子统计 -->
      <span id="busuanzi_container_site_pv">
        网站总访问量:<span id="busuanzi_value_site_pv"></span>
      </span>
      
     <span class="post-meta-divider">|</span>
      <span id="busuanzi_container_site_uv" style='display:none'>
          本站访客数<span id="busuanzi_value_site_uv"></span>人
      </span>
    <script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
     
  </div>
</footer>

            </div>
            <!-- styles -->



  <link rel="preload" as="style" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" crossorigin="anonymous" onload="this.onload=null;this.rel='stylesheet'"/>


              <!-- jquery -->

  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js" crossorigin="anonymous"></script>




<!-- clipboard -->

  
    <script src="https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/2.0.7/clipboard.min.js" crossorigin="anonymous"></script>
  
  <script type="text/javascript">
  $(function() {
    // copy-btn HTML
    var btn = "<span class=\"btn-copy tooltipped tooltipped-sw\" aria-label=\"Copy to clipboard!\">";
    btn += '<i class="fa-regular fa-clone"></i>';
    btn += '</span>';
    // mount it!
    $(".highlight table").before(btn);
    var clip = new ClipboardJS('.btn-copy', {
      text: function(trigger) {
        return Array.from(trigger.nextElementSibling.querySelectorAll('.code')).reduce((str,it)=>str+it.innerText+'\n','')
      }
    });
    clip.on('success', function(e) {
      e.trigger.setAttribute('aria-label', "Copied!");
      e.clearSelection();
    })
  })
  </script>


<script src="/mrl/js/main.js"></script>

<!-- search -->

<!-- Baidu Analytics -->

<!-- Cloudflare Analytics -->

<!-- Disqus Comments -->

<!-- utterances Comments -->

    </body>

    </html>